package cn.liyiming.bbsadmin.modular.system.controller;

import cn.liyiming.bbsadmin.modular.system.model.Article;
import cn.liyiming.bbsadmin.modular.system.model.User;
import cn.liyiming.bbsadmin.modular.system.service.impl.ArticleServiceImpl;
import cn.liyiming.bbsadmin.modular.system.service.impl.UserServiceImpl;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @program: bbsblog
 * @description: 控制台
 * @author: 李一鸣
 * @create: 2020-04-10 10:01
 **/
@Controller
@RequestMapping("/console")
public class ConsoleController extends BaseController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private ArticleServiceImpl articleService;

    @RequestMapping("")
    public String console(Model model) {
        List<User> users = userService.selectList(null);
        int userNum = users.size();
        List<Article> articles = articleService.selectList(null);
        int articleNum = articles.size();
        model.addAttribute("userNum",userNum);
        model.addAttribute("articleNum",articleNum);
        return "/console.html";
    }

    @RequestMapping("/data")
    @ResponseBody
    public Object data(Model model) {
        System.out.println("_____________________________");
        List<User> users = userService.selectList(null);
        int userNum = users.size();
        List<Article> articles = articleService.selectList(null);
        int articleNum = articles.size();
        model.addAttribute("userNum",userNum);
        model.addAttribute("articleNum",articleNum);
        HashMap data = new HashMap(16);
        data.put("userNum",userNum);
        data.put("articleNum",articleNum);
        return ResponseData.success(200,"成功",data);
    }
}
